package xin.cms.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xin.cms.model.Article;
import xin.cms.request.ArticlePageRequest;
import xin.cms.service.ArticleService;
import xin.common.http.response.PageResponse;
import xin.common.http.response.Response;

/**
 * <pre>
 * 文章管理
 * </pre>
 *
 * @author lixin_ma@outlook.com
 * @version 1.0
 * @since 2018/8/9
 */
@RestController
@RequestMapping("article")
@Api(value = "xin-cms", description = "文章管理", position = 3)
@Slf4j
public class ArticleController {

    @Autowired
    private ArticleService service;

    @ApiOperation(value = "文章管理" , notes = "文章管理",response = PageResponse.class )
    @GetMapping("pageList")
    public PageResponse articleList(ArticlePageRequest request){
        return service.findPageList(request);
    }

    @ApiOperation(value = "添加文章" , notes = "添加文章",response = Response.class )
    @PutMapping()
    public Response putArticle(@RequestBody Article article){
        return editArticle(article);
    }

    @ApiOperation(value = "编辑文章" , notes = "编辑文章",response = Response.class )
    @PostMapping
    public Response postArticle(@RequestBody Article article){
        return editArticle(article);
    }

    @ApiOperation(value = "查看文章" , notes = "查看文章",response = Response.class )
    @GetMapping("{articleId}")
    public Response getArticle(@PathVariable String articleId){
        Response<Article> response = new Response();
        response.setSuccess(Boolean.TRUE);
        response.setData(service.findOne(articleId));
        return response;
    }

    private Response editArticle(Article article){
        Response<Article> response = new Response();
        response.setSuccess(Boolean.TRUE);
        response.setData(service.save(article));
        return response;
    }

}
